package cn.xfdzx.android.apps.shop.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.bean.GoodsBeen;
import cn.xfdzx.android.apps.shop.contract.GoodActionCallback;
import cn.xfdzx.android.apps.shop.contract.ItemClickListener;
import cn.xfdzx.android.apps.shop.util.Utils;

/**
 * @author gzk
 * @date 2020/3/27 0027
 * description：社区零售的适配器
 */
public class ShopGoodsItemAdapter extends RecyclerView.Adapter {

    private ItemClickListener mItemClickListener;

    /**
     * 类型1：标题头
     * banner实现
     */
    public static final int TYPE_TITLE = 0;
    /**
     * 类型2：商品内容布局
     */
    public static final int TYPE_CONTENT = 1;

    private ArrayList<GoodsBeen> mList;
    /**
     * 当前类型
     */
    public int mCurrentType = TYPE_TITLE;

    private Context mContext;
    private final LayoutInflater mLayoutInflater;
    private  GoodActionCallback callback;

    public ShopGoodsItemAdapter(Context mContext, ArrayList<GoodsBeen> mList, GoodActionCallback callback) {
        this.mContext = mContext;
        this.mList = mList;
        this.callback = callback;
        //以后用它来初始化布局
        mLayoutInflater = LayoutInflater.from(mContext);

    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == TYPE_TITLE) {
            return new ShopTitleViewHolder(mContext, mLayoutInflater.inflate(R.layout.item_title, null));
        }else if (viewType == TYPE_CONTENT) {
            return new ShopContentViewHolder(mContext, mLayoutInflater.inflate(R.layout.item_selling_goods_right, null),callback);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {

        int type = getItemViewType(position);
        switch (type){
            case TYPE_TITLE:
                ((ShopTitleViewHolder)holder).setData(mList.get(position));
                break;

           case TYPE_CONTENT:
//               ((ShopContentViewHolder)holder).setData(mList.get(position),position);
               holder.itemView.setOnClickListener(new View.OnClickListener() {
                   @Override
                   public void onClick(View view) {
                       if (Utils.isFastClick()) {
                           mItemClickListener.onItemClick(position);
                       }
                   }
               });
                break;

        }

    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (mList.get(position).isTitle){
            mCurrentType = TYPE_TITLE;
        }else {
            mCurrentType = TYPE_CONTENT;
        }
        return mCurrentType;
    }




    public void setOnItemClickListener(ItemClickListener itemClickListener) {
        this.mItemClickListener = itemClickListener;

    }
}
